using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;


public partial class ProductSellDetail : System.Web.UI.Page
{
    protected long Id
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Id"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["Id"]);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        LoadProductDetails();
        LoadOtherPro();
        LoadAddMore();
    }
    #region Load Product detail
    private void LoadProductDetails()
    {
        ProductSell _ProductSell = ProductSellService.GetProductSell(Id);
        if (_ProductSell != null)
        {
            UnitPrice _UnitPrice = UnitPriceService.GetUnitPrice(_ProductSell.FOBPriceId);
            ltrTitleProduct.Text = _ProductSell.Title;
            ProductCat _ProductCatSubSub = ProductCatService.GetProductCat(_ProductSell.CatId);
            if (_ProductCatSubSub != null)
            {
                hrfSubSubCat.HRef = "ProductList.aspx?idSubSubcat=" + _ProductCatSubSub.ProCateID + "";
                ltrSubSubCat.Text = _ProductCatSubSub.Name;
            }
            ProductCat _ProductCatSub = ProductCatService.GetProductCat(_ProductCatSubSub.Parrentid);
            if (_ProductCatSub != null)
            {
                hrfSubCat.HRef = "ProductList.aspx?idSubcat=" + _ProductCatSub.ProCateID + "";
                ltrSubCat.Text = _ProductCatSub.Name;
            }
            ProductCat _ProductCat = ProductCatService.GetProductCat(_ProductCatSub.Parrentid);
            if (_ProductCat != null)
            {
                hrfCat.HRef = "ProductList.aspx?idcat=" + _ProductCat.ProCateID + "";
                ltrCat.Text = _ProductCat.Name;
            }
            string strUnitPrice = "";
            if (_UnitPrice != null)
                strUnitPrice = _UnitPrice.Title;
            ADESSA.Entities.UnitType _UnitType1 = UnitTypeService.GetUnitType(_ProductSell.PerTypeId);
            string strUnitType1 = "";
            if (_UnitType1 != null)
                strUnitType1 = _UnitType1.Name;
            ltrFOBPrice.Text = strUnitPrice + " " + _ProductSell.PriceSmall + " - " + _ProductSell.PriceLarge + " " + strUnitType1;
            ltrPort.Text = _ProductSell.Port;
            ADESSA.Entities.UnitType _UnitType = UnitTypeService.GetUnitType(_ProductSell.MinQuantityTypeId);
            string strUnitType = "";
            string MinOtherQuantity = " Other " + _ProductSell.MinQuantityOther;
            if (_UnitType != null)
            {
                strUnitType = _UnitType.Name;
            }
            ltrMinOrderQuantity.Text = _ProductSell.MinOrderQuantity + " " + strUnitType + MinOtherQuantity;
            //ltrMinOrderQuantity
            ltrPaymentTerms.Text = _ProductSell.PaymentTerms;
            if (!string.IsNullOrEmpty(_ProductSell.Image))
                imgdetail.Src = "photos/" + _ProductSell.Image;
            #region Load about company
            Member _Member = MemberService.GetMember(_ProductSell.MemberId);
            #region Load info company
            Criteria cri = new Criteria();
            cri.add(CompanyColumns.MemberID, _Member.MemberID, Condition.EQUAL, Condition.AND);
            CompanyCollection _CompanyCollection = CompanyService.GetCompanyListByParam(CompanyColumns.CompanyID, OrderDirection.ASC, cri.Criter);
            hrfCompanyName.HRef = "miniweb/Default.aspx?id=" + _CompanyCollection[0].CompanyID + "";
            ltrCompanyName.Text = _CompanyCollection[0].CompanyName;
            hrfCompany.HRef = "miniweb/Default.aspx?id=" + _CompanyCollection[0].CompanyID + "";
            ltrCompanyInfo.Text = Utilitys.ShortSummary(400, _CompanyCollection[0].CompanyInfoHome);
            ltrNumberOfEmployees.Text = _CompanyCollection[0].NumberEmployees;
            ltrFactoryInformation.Text = _CompanyCollection[0].InfoFactory;
            #endregion

            #region Load Type company
            Criteria crit = new Criteria();
            crit.add(CompanyAndTypeComColumns.CompanyID, _CompanyCollection[0].CompanyID, Condition.EQUAL, Condition.AND);
            CompanyAndTypeComCollection _CompanyAndTypeComCollection = CompanyAndTypeComService.GetCompanyAndTypeComListByParam(CompanyAndTypeComColumns.CompanyAndTypeComID, OrderDirection.ASC, crit.Criter);
            string TitleCompanyTyle = "";
            if (_CompanyAndTypeComCollection.Count > 0)
            {
                for (int i = 0; i < _CompanyAndTypeComCollection.Count; i++)
                {
                    TypeCompany _TypeCompany = TypeCompanyService.GetTypeCompany(_CompanyAndTypeComCollection[i].TypeComID);
                    TitleCompanyTyle += _TypeCompany.Title + ", ";
                }
            }
            ltrCompanyType.Text = TitleCompanyTyle;
            #endregion
            #region Load Type Business
            Criteria critr = new Criteria();
            critr.add(CompanyAndBusinesColumns.CompanyID, _CompanyCollection[0].CompanyID, Condition.EQUAL, Condition.AND);
            CompanyAndBusinesCollection _CompanyAndBusinesCollection = CompanyAndBusinesService.GetCompanyAndBusinesListByParam(CompanyAndBusinesColumns.CompanyAndBusinessID, OrderDirection.ASC, critr.Criter);
            string TitleCompanyTypeBusiness = "";
            if (_CompanyAndBusinesCollection.Count > 0)
            {
                for (int i = 0; i < _CompanyAndBusinesCollection.Count; i++)
                {
                    TypeBusines _TypeBusines = TypeBusinesService.GetTypeBusines(_CompanyAndBusinesCollection[i].BusinessID);
                    TitleCompanyTypeBusiness += _TypeBusines.Title + ", ";
                }
            }
            ltrBussinessType.Text = TitleCompanyTypeBusiness;
            #endregion
            Country _Country = CountryService.GetCountry(_CompanyCollection[0].Country);
            if (!string.IsNullOrEmpty(_Country.Img))
                imgflag.Src = "photos/" + _Country.Img;
            ltrContryName.Text = _Country.Name;
            if (_Member.GoldMember == true)
            {
                ltrMemberTyle.Text = "Gold Member";
            }
            else
            {
                imggold.Visible = false;
                ltrMemberTyle.Text = " Free Member";
            }
            ltryahoo.Text = _Member.Other;
            if (!string.IsNullOrEmpty(_Member.Other))
            {
                if (_Member.Status == true)
                {
                    if (Utility.GetYahooStatus(_Member.Other))
                    {
                        imgyahoo.Src = "images/icon_online.png";
                    }
                    else
                    {
                        imgyahoo.Src = "images/icon_offline.png";
                    }
                    hryahoo.HRef = "ymsgr:sendIM?" + (_Member.Other);
                }
                else
                {
                    if (Utility.GetYahooStatus(_Member.Other))
                    {
                        imgyahoo.Src = "images/skype.png";
                    }
                    else
                    {
                        imgyahoo.Src = "images/skype_off.png";
                    }
                    hryahoo.HRef = "skype://" + _Member.Other + "?chat";
                }
            }


            #endregion
            ltrQuickDetail.Text = _ProductSell.ProductDescription;
            ltrPackagingDelivery.Text = _ProductSell.PackagingDetail;
            ltrSpecifications.Text = _ProductSell.Description;


        }
    }
    #region Load Add MoreProduct
    int coutcollectadd = 0;
    private void LoadAddMore()
    {
        int Outtotal = 0;        
        Criteria cri = new Criteria();
        
        cri.add(AddMoreSellProductColumns.ProductId, Id, Condition.EQUAL, Condition.AND);
        AddMoreSellProductCollection AddMoreSellProductCollection = AddMoreSellProductService.GetAddMoreSellProductListByParam(AddMoreSellProductColumns.AddMoreId, OrderDirection.ASC, cri.Criter);
        coutcollectadd = AddMoreSellProductCollection.Count;
        rptAddMore.DataSource = AddMoreSellProductCollection;
        rptAddMore.DataBind();
    }
    protected void rptAddMore_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AddMoreSellProduct _AddMoreSellProduct = (AddMoreSellProduct)e.Item.DataItem;
           
            Literal ltrTitle = (Literal)e.Item.FindControl("ltrTitle");
            Literal ltrName = (Literal)e.Item.FindControl("ltrName");
            Literal ltrdiv = (Literal)e.Item.FindControl("ltrdiv");
            ltrTitle.Text = _AddMoreSellProduct.Title;
            ltrName.Text = _AddMoreSellProduct.Name;

            int countindex = e.Item.ItemIndex + 1;
            if (countindex % 3 == 0)
                ltrdiv.Text = "<div class='clear'></div>";
            else if (coutcollect == countindex)
                ltrdiv.Text = "<div class='clear'></div>";           
        }
    }
    #endregion
    #endregion
    #region Load Other Proudct
    int coutcollect = 0;
    private void LoadOtherPro()
    {
        int Outtotal = 0;
        ProductSell _ProductSell = ProductSellService.GetProductSell(Id);
        Criteria cri = new Criteria();
        cri.add(ProductSellColumns.MemberId, _ProductSell.MemberId, Condition.EQUAL, Condition.AND);
        cri.add(ProductSellColumns.ProductId, Id, Condition.NOT_EQUAL, Condition.AND);
        ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam(ProductSell._Table + "." + ProductSellColumns.CreatedDate, OrderDirection.DESC, 1, 10, cri.Criter, out Outtotal);
        coutcollect = _ProductSellCollection.Count;
        rptOtherPro.DataSource = _ProductSellCollection;
        rptOtherPro.DataBind();
    }
    protected void rptOtherPro_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ProductSell _ProductSell = (ProductSell)e.Item.DataItem;
            HtmlAnchor hrfimg = (HtmlAnchor)e.Item.FindControl("hrfimg");
            HtmlAnchor hrfTitle = (HtmlAnchor)e.Item.FindControl("hrfTitle");
            HtmlImage img = (HtmlImage)e.Item.FindControl("img");
            Literal ltrTitle = (Literal)e.Item.FindControl("ltrTitle");
            Literal ltrdiv = (Literal)e.Item.FindControl("ltrdiv");
            hrfimg.HRef = "ProductsellDetail-" + _ProductSell.ProductId + "-" + Utilitys.StripDiacritics(Utility.Splitword(_ProductSell.Title)) + ".aspx";
            hrfTitle.HRef = "ProductsellDetail-" + _ProductSell.ProductId + "-" + Utilitys.StripDiacritics(Utility.Splitword(_ProductSell.Title)) + ".aspx";
            if (!string.IsNullOrEmpty(_ProductSell.Image))
                img.Src = "photos/" + _ProductSell.Image;
            ltrTitle.Text = Utilitys.ShortSummary(50, _ProductSell.Title);
            int countindex = e.Item.ItemIndex + 1;
            if (countindex % 5 == 0)
                ltrdiv.Text = "<div class='clr'></div>";
            else if (coutcollect == countindex)
                ltrdiv.Text = "<div class='clr'></div>";
            else
                ltrdiv.Text = "<li class='doc'></li>";

        }
    }
    #endregion
}
